package org.jmh.gift.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import org.jmh.gift.domain.PayFlow;
import org.jmh.gift.domain.PayOrder;
import org.jmh.gift.mapper.PayOrderMapper;
import org.jmh.gift.service.IPayAliService;
import org.jmh.gift.service.IPayFlowService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class IPayAliServiceImpl implements IPayAliService {
    @Autowired
    private IPayFlowService payFlowService;
    @Autowired
    private PayOrderMapper payOrderMapper;

    @Override
    public void aliPayExecute(JSONObject jsonObject) {
        //修改支付单的状态
        String uniPayOrderSn = jsonObject.getString("out_trade_no");
        payOrderMapper.updateByStatus(1,uniPayOrderSn);
        //将流水添加到支付信息里
        PayFlow payFlow = new PayFlow();
        payFlow.setNotifyTime(jsonObject.getDate("notify_time"));
        payFlow.setSubject(jsonObject.getString("subject"));
        payFlow.setOutTradeNo(jsonObject.getString("trade_no"));
        payFlow.setTradeNo(uniPayOrderSn);
        payFlow.setTotalAmount(jsonObject.getBigDecimal("total_amount"));
        payFlow.setTradeStatus(jsonObject.getString("trade_status"));
        payFlow.setPassbackParams(jsonObject.getString("passback_params"));
        payFlow.setPaySuccess(true);
        payFlow.setResultDesc("支付成功!!!!!");
        payFlowService.insert(payFlow);
    }

    @Override
    public Boolean aliPayCheck(JSONObject jsonObject) {
        String uniPayOrderSn = jsonObject.getString("out_trade_no");
        Integer s = payOrderMapper.selectCount(new EntityWrapper<PayOrder>().eq("uni_pay_order_sn", uniPayOrderSn).and().eq("pay_status",1));
        return s > 0;
    }
}
